package Controller;

import Pojo.SnackOrder;
import Service.impl.SnackOrderServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

public class SnackOrderAddOneServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        try {
            HashMap<Object,Object> resultMap=new HashMap<>();
            String snackid = request.getParameter("snackid");
            String uid = request.getParameter("uid");
            String num = request.getParameter("num");
            String allprice = request.getParameter("allprice");
            //先去新增一个零食订单
            SnackOrderServiceImpl  snackOrderService=new SnackOrderServiceImpl();
            SnackOrder snackOrder=new SnackOrder();
            snackOrder.setSnackid(Integer.parseInt(snackid));
            snackOrder.setUid(Integer.parseInt(uid));
            snackOrder.setNum(Integer.parseInt(num));
            snackOrder.setAllprice(Integer.parseInt(allprice));
            HashMap<Object,Object> map=snackOrderService.AddOne(snackOrder);
            Integer integer= (Integer) map.get("addResult");
            String s=integer>0?"success":"fail";
            //将新增结果和二维码结果放入map结果集中
            resultMap.put("addResult",s);
            resultMap.put("qrResult",map.get("qrResult"));
            resultMap.put("snackorder",map.get("snackorder"));
            response.getWriter().print(JSON.toJSONString(resultMap));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
